// exports.AL_Framework_Release_Manager = void 0;
// var $macros = require("./AL_Framework_Macros");
// var n = "[AL_Framework_Release_Manager] : ";
// var a = (function () {
//     function e(e) {
//         this.name = "";
//         this._assets = new Map();
//         this.name = e;
//     }
//     e.prototype.add = function (e) {
//         if (Array.isArray(e.data)) {
//             Log.d(n + "向" + this.name + "加入待释放目录:" + e.url);
//             for (var t = 0; t < e.data.length; t++) {
//                 if (e.data[t]) {
//                     e.data[t].addRef();
//                 }
//             }
//         } else {
//             if (e.data) {
//                 Log.d(n + "向" + this.name + "加入待释放资源:" + e.url);
//                 e.data.addRef();
//             }
//         }
//         this._assets.set(e.url, e);
//     };
//     e.prototype.get = function (e) {
//         var t = this._assets.get(e);
//         var o = null;
//         if (t) {
//             if (Array.isArray(t.data)) {
//                 for (var i = 0; i < t.data.length; i++) {
//                     if (t.data[i]) {
//                         t.data[i].decRef();
//                     }
//                 }
//                 Log.d(n + "向" + this.name + "获取待释放目录:" + t.url);
//                 o = t.data;
//             } else {
//                 if (cc.isValid(t.data)) {
//                     t.data.decRef();
//                     Log.d(n + "向" + this.name + "获取待释放资源:" + t.url);
//                     o = t.data;
//                 }
//             }
//         }
//         this._assets.delete(e);
//         return o;
//     };
//     e.prototype.chuli_neicunbuzu_shi_shifang_ziyuan = function () {
//         var e = this;
//         if (this._assets.size > 0) {
//             Log.d(n + "bundle : " + this.name + " 释放加载的资源");
//             if (this.name == $macros.Macro.BUNDLE_REMOTE) {
//                 this._assets.forEach(function (t) {
//                     if (t.data instanceof cc.Asset) {
//                         Log.d(n + "bundle : " + e.name + " 释放远程加载资源" + t.url);
//                         cc.assetManager.releaseAsset(t.data);
//                     }
//                 });
//                 return void this._assets.clear();
//             }
//             var t = cc.assetManager.getBundle(this.name);
//             t
//                 ? (this._assets.forEach(function (o) {
//                       if (Array.isArray(o.data)) {
//                           Log.d(n + "bundle : " + e.name + " 释放加载目录" + o.url);
//                           for (var i = 0; i < o.data.length; i++) {
//                               if (o.data[i]) {
//                                   o.data[i].decRef();
//                                   var a = o.url + "/" + o.data[i].name;
//                                   if (!(null == t)) {
//                                       t.release(a, o.type);
//                                   }
//                                   Log.d(n + "bundle : " + e.name + " 释放加载资源" + a);
//                               }
//                           }
//                       } else {
//                           if (cc.isValid(o.data)) {
//                               o.data.decRef();
//                               if (!(null == t)) {
//                                   t.release(o.url, o.type);
//                               }
//                               Log.d(n + "bundle : " + e.name + " 释放加载资源" + o.url);
//                           }
//                       }
//                   }),
//                   this._assets.clear())
//                 : (Log.w(n + "释放bundle : " + this.name + " 时，Bundle已经被释放，直接清空待释放数据"),
//                   this._assets.clear());
//         }
//     };
//     e.prototype.changshi_yichu = function (e) {
//         if (this.name == e) {
//             this.chuli_neicunbuzu_shi_shifang_ziyuan();
//         }
//     };
//     Object.defineProperty(e.prototype, "assets", {
//         get: function () {
//             return this._assets;
//         },
//         enumerable: !1,
//         configurable: !0
//     });
//     return e;
// })();
// var s = (function () {
//     function e() {
//         this._lazyInfos = new Map();
//         this._bundles = new Map();
//         this._remote = new a($macros.Macro.BUNDLE_REMOTE);
//     }
//     e.Instance = function () {
//         return this._instance || (this._instance = new e());
//     };
//     e.prototype.getBundle = function (e) {
//         return Manager.bundleManager.getBundle(e);
//     };
//     e.prototype.huoquZiyuanbaoMingzi = function (e) {
//         return Manager.bundleManager.huoquZiyuanbaoMingzi(e);
//     };
//     e.prototype.release = function (e) {
//         var t = this.getBundle(e.bundle);
//         if (t) {
//             if (Manager.shifou_yanchi_shifang_ziyuan && 0 != e.shifou_yanchi_shifang_ziyuan) {
//                 var o = t.name;
//                 var i = void 0;
//                 this._lazyInfos.has(o) ? (i = this._lazyInfos.get(o)) : ((i = new a(o)), this._lazyInfos.set(o, i));
//                 if (i) {
//                     i.add(e);
//                 }
//             } else {
//                 if (Array.isArray(e.data)) {
//                     for (var s = 0; s < e.data.length; s++) {
//                         var r = e.url + "/" + e.data[s].name;
//                         t.release(r, e.type);
//                     }
//                     Log.d(n + "成功释放资源目录 : " + e.bundle + "." + e.url);
//                 } else {
//                     t.release(e.url, e.type);
//                     Log.d(n + "成功释放资源 : " + e.bundle + "." + e.url);
//                 }
//             }
//         } else {
//             Log.e("" + n + e.bundle + " no found");
//         }
//     };
//     e.prototype.get = function (e, t) {
//         var o = this.getBundle(e);
//         if (o) {
//             var i = this._lazyInfos.get(o.name);
//             if (i) {
//                 return i.get(t);
//             }
//         } else {
//             Log.w("" + n + e + "不存在，删除释放管理器中的缓存");
//             var a = this.huoquZiyuanbaoMingzi(e);
//             this.chuli_jiazai_ziyuanbao(a);
//             this._lazyInfos.delete(a);
//         }
//         return null;
//     };
//     e.prototype.removeBundle = function (e, t) {
//         if (void 0 === t) {
//             t = !0;
//         }
//         var o = this.getBundle(e);
//         Manager.shifou_yanchi_shifang_ziyuan && t
//             ? o &&
//               (Log.d(n + "向释放管理器中添加待释放bundle : " + (null == o ? void 0 : o.name)),
//               this._bundles.set(null == o ? void 0 : o.name, !1))
//             : (Log.d(n + "释放Bundle : " + (null == o ? void 0 : o.name)), o && cc.assetManager.removeBundle(o));
//     };
//     e.prototype.chuli_jiazai_ziyuanbao = function (e) {
//         this._bundles.delete(e);
//     };
//     e.prototype.chuli_neicunbuzu_shi_shifang_ziyuan = function () {
//         var e = this;
//         Log.d(n + "------------收到内存警告，释放无用资源------------");
//         this._lazyInfos.forEach(function (e) {
//             e.chuli_neicunbuzu_shi_shifang_ziyuan();
//         });
//         Log.d(n + "-------------释放无用bundle-------------");
//         this._bundles.forEach(function (t, o) {
//             var i = cc.assetManager.getBundle(o);
//             if (i) {
//                 Log.d("释放无用bundle : " + o);
//                 cc.assetManager.removeBundle(i);
//                 e._bundles.delete(o);
//             }
//         });
//         Log.d(n + "-------------释放无用远程资源-------------");
//         this._remote.chuli_neicunbuzu_shi_shifang_ziyuan();
//     };
//     e.prototype.changshi_yichu_ziyuanbao = function (e) {
//         Log.d(n + "--------------尝试释放" + e + "加载资源------------");
//         this._lazyInfos.forEach(function (t) {
//             t.changshi_yichu(e);
//         });
//         var t = this.huoquZiyuanbaoMingzi(e);
//         var o = cc.assetManager.getBundle(t);
//         if (o) {
//             Log.d("释放无用bundle : " + t);
//             cc.assetManager.removeBundle(o);
//             this._bundles.delete(t);
//         }
//     };
//     e.prototype.huoqu_yuancheng_ziyuan = function (e) {
//         return this._remote.get(e);
//     };
//     e.prototype.shifang_yuancheng_ziyuan = function (e) {
//         Manager.shifou_yanchi_shifang_ziyuan && 0 != e.shifou_yanchi_shifang_ziyuan
//             ? this._remote.add(e)
//             : e.data instanceof cc.Asset && cc.assetManager.releaseAsset(e.data);
//     };
//     e.prototype.print = function (e) {
//         var t = [];
//         this._bundles.forEach(function (e, o) {
//             t.push(o);
//         });
//         e.print({
//             lazyInfo: this._lazyInfos,
//             bundles: t,
//             remote: this._remote
//         });
//     };
//     e._instance = null;
//     return e;
// })();
// exports.AL_Framework_Release_Manager = s;


exports.AL_Framework_Release_Manager = void 0;
const $macros = require("./AL_Framework_Macros");
const logPrefix = "[AL_Framework_Release_Manager] : ";

/** 资源管理类 */
class AssetManager {
    
    constructor(name) {
        this.name = name || "";
        this._assets = new Map();
    }

    /**
     * 添加资源到待释放列表
     * @param {Object} resource - 资源对象
     */
    add(resource) {
        if (Array.isArray(resource.data)) {
            Log.d(`${logPrefix}向${this.name}加入待释放目录: ${resource.url}`);
            resource.data.forEach(asset => asset && asset.addRef());
        } else if (resource.data) {
            Log.d(`${logPrefix}向${this.name}加入待释放资源: ${resource.url}`);
            resource.data.addRef();
        }
        this._assets.set(resource.url, resource);
    }

    /**
     * 获取资源并减少引用计数
     * @param {string} url - 资源URL
     * @returns 资源对象或目录
     */
    get(url) {
        const resource = this._assets.get(url);
        let result = null;
        if (resource) {
            if (Array.isArray(resource.data)) {
                resource.data.forEach(asset => asset && asset.decRef());
                Log.d(`${logPrefix}向${this.name}获取待释放目录: ${resource.url}`);
                result = resource.data;
            } else if (cc.isValid(resource.data)) {
                resource.data.decRef();
                Log.d(`${logPrefix}向${this.name}获取待释放资源: ${resource.url}`);
                result = resource.data;
            }
        }
        this._assets.delete(url);
        return result;
    }

    /** 处理内存不足时的资源释放 */
    chuli_neicunbuzu_shi_shifang_ziyuan() {
        if (this._assets.size > 0) {
            Log.d(`${logPrefix}bundle : ${this.name} 释放加载的资源`);
            if (this.name === $macros.Macro.BUNDLE_REMOTE) {
                this._assets.forEach((resource) => {
                    if (resource.data instanceof cc.Asset) {
                        Log.d(`${logPrefix}bundle : ${this.name} 释放远程加载资源${resource.url}`);
                        cc.assetManager.releaseAsset(resource.data);
                    }
                });
                this._assets.clear();
                return;
            }
            const bundle = cc.assetManager.getBundle(this.name);
            if (bundle) {
                this._assets.forEach((resource) => {
                    if (Array.isArray(resource.data)) {
                        Log.d(`${logPrefix}bundle : ${this.name} 释放加载目录${resource.url}`);
                        resource.data.forEach(asset => {
                            if (asset) {
                                asset.decRef();
                                const assetUrl = `${resource.url}/${asset.name}`;
                                bundle.release(assetUrl, resource.type);
                                Log.d(`${logPrefix}bundle : ${this.name} 释放加载资源${assetUrl}`);
                            }
                        });
                    } else if (cc.isValid(resource.data)) {
                        resource.data.decRef();
                        bundle.release(resource.url, resource.type);
                        Log.d(`${logPrefix}bundle : ${this.name} 释放加载资源${resource.url}`);
                    }
                });
                this._assets.clear();
            } else {
                Log.w(`${logPrefix}释放bundle : ${this.name} 时，Bundle已经被释放，直接清空待释放数据`);
                this._assets.clear();
            }
        }
    }

    /**
     * 尝试移除指定名称的资源
     * @param {string} name - 资源名称
     */
    changshi_yichu(name) {
        if (this.name === name) {
            this.chuli_neicunbuzu_shi_shifang_ziyuan();
        }
    }

    /** 获取当前资源集 */
    get assets() {
        return this._assets;
    }
}

/** 释放管理器单例类 */
class AL_Framework_Release_Manager {

    constructor() {
        this._lazyInfos = new Map();
        this._bundles = new Map();
        this._remote = new AssetManager($macros.Macro.BUNDLE_REMOTE);
    }

    /** 获取管理器单例 */
    static Instance() {
        return this._instance || (this._instance = new AL_Framework_Release_Manager());
    }

    /**
     * 获取资源包
     * @param {string} name - 包名
     * @returns 包对象
     */
    getBundle(name) {
        return Manager.bundleManager.getBundle(name);
    }

    /**
     * 获取资源包名称
     * @param {string} name - 包名
     * @returns 包名
     */
    huoquZiyuanbaoMingzi(name) {
        return Manager.bundleManager.huoquZiyuanbaoMingzi(name);
    }

    /**
     * 释放资源
     * @param {Object} resource - 资源对象
     */
    release(resource) {
        const bundle = this.getBundle(resource.bundle);
        if (bundle) {
            if (Manager.shifou_yanchi_shifang_ziyuan && resource.shifou_yanchi_shifang_ziyuan !== 0) {
                const bundleName = bundle.name;
                let lazyInfo = this._lazyInfos.get(bundleName) || new AssetManager(bundleName);
                this._lazyInfos.set(bundleName, lazyInfo);
                lazyInfo.add(resource);
            } else {
                if (Array.isArray(resource.data)) {
                    resource.data.forEach(asset => {
                        const assetUrl = `${resource.url}/${asset.name}`;
                        bundle.release(assetUrl, resource.type);
                    });
                    Log.d(`${logPrefix}成功释放资源目录 : ${resource.bundle}.${resource.url}`);
                } else {
                    bundle.release(resource.url, resource.type);
                    Log.d(`${logPrefix}成功释放资源 : ${resource.bundle}.${resource.url}`);
                }
            }
        } else {
            Log.e(`${logPrefix}${resource.bundle} no found`);
        }
    }

    /**
     * 获取资源
     * @param {string} bundleName - 包名
     * @param {string} url - 资源URL
     * @returns 资源对象或null
     */
    get(bundleName, url) {
        const bundle = this.getBundle(bundleName);
        if (bundle) {
            const lazyInfo = this._lazyInfos.get(bundle.name);
            if (lazyInfo) {
                return lazyInfo.get(url);
            }
        } else {
            Log.w(`${logPrefix}${bundleName}不存在，删除释放管理器中的缓存`);
            const bundleRealName = this.huoquZiyuanbaoMingzi(bundleName);
            this.chuli_jiazai_ziyuanbao(bundleRealName);
            this._lazyInfos.delete(bundleRealName);
        }
        return null;
    }

    /**
     * 移除资源包
     * @param {string} bundleName - 包名
     * @param {boolean} [isLazy=true] - 是否延迟
     */
    removeBundle(bundleName, isLazy = true) {
        const bundle = this.getBundle(bundleName);
        if (Manager.shifou_yanchi_shifang_ziyuan && isLazy) {
            if (bundle) {
                Log.d(`${logPrefix}向释放管理器中添加待释放bundle : ${bundle.name}`);
                this._bundles.set(bundle.name, false);
            }
        } else {
            if (bundle) {
                Log.d(`${logPrefix}释放Bundle : ${bundle.name}`);
                cc.assetManager.removeBundle(bundle);
            }
        }
    }

    /**
     * 处理包加载
     * @param {string} bundleName - 包名
     */
    chuli_jiazai_ziyuanbao(bundleName) {
        this._bundles.delete(bundleName);
    }

    /** 处理内存不足时的资源释放 */
    chuli_neicunbuzu_shi_shifang_ziyuan() {
        Log.d(`${logPrefix}------------收到内存警告，释放无用资源------------`);
        this._lazyInfos.forEach(lazyInfo => lazyInfo.chuli_neicunbuzu_shi_shifang_ziyuan());
        Log.d(`${logPrefix}-------------释放无用bundle-------------`);
        this._bundles.forEach((_, bundleName) => {
            const bundle = cc.assetManager.getBundle(bundleName);
            if (bundle) {
                Log.d(`释放无用bundle : ${bundleName}`);
                cc.assetManager.removeBundle(bundle);
                this._bundles.delete(bundleName);
            }
        });
        Log.d(`${logPrefix}-------------释放无用远程资源-------------`);
        this._remote.chuli_neicunbuzu_shi_shifang_ziyuan();
    }

    /**
     * 尝试释放指定包的资源
     * @param {string} bundleName - 包名
     */
    changshi_yichu_ziyuanbao(bundleName) {
        Log.d(`${logPrefix}--------------尝试释放${bundleName}加载资源------------`);
        this._lazyInfos.forEach(lazyInfo => lazyInfo.changshi_yichu(bundleName));
        const realBundleName = this.huoquZiyuanbaoMingzi(bundleName);
        const bundle = cc.assetManager.getBundle(realBundleName);
        if (bundle) {
            Log.d(`释放无用bundle : ${realBundleName}`);
            cc.assetManager.removeBundle(bundle);
            this._bundles.delete(realBundleName);
        }
    }

    /**
     * 获取远程资源
     * @param {string} url - 资源URL
     * @returns 资源对象或null
     */
    huoqu_yuancheng_ziyuan(url) {
        return this._remote.get(url);
    }

    /**
     * 释放远程资源
     * @param {Object} resource - 资源对象
     */
    shifang_yuancheng_ziyuan(resource) {
        if (Manager.shifou_yanchi_shifang_ziyuan && resource.shifou_yanchi_shifang_ziyuan !== 0) {
            this._remote.add(resource);
        } else if (resource.data instanceof cc.Asset) {
            cc.assetManager.releaseAsset(resource.data);
        }
    }

    /**
     * 打印当前资源状态
     * @param {Object} printer - 打印机对象
     */
    print(printer) {
        const bundleNames = [];
        this._bundles.forEach((_, bundleName) => bundleNames.push(bundleName));
        printer.print({
            lazyInfo: this._lazyInfos,
            bundles: bundleNames,
            remote: this._remote
        });
    }
}

AL_Framework_Release_Manager._instance = null;
exports.AL_Framework_Release_Manager = AL_Framework_Release_Manager;
